<!--
 * @Author: ljc
 * @Date: 2023-04-25 15:00:24
 * @LastEditors: g05047
 * @LastEditTime: 2023-05-11 10:03:18
 * @Description: 备选项目上报
-->

<template>
  <RkContainer :isFooter="false">
    <template #header_left>
      <RkButtonGroup :options="json_sb_button"
        v-model="queryParams.wfStatus"
        @click="handleSearch" />
    </template>
    <template #header_right>
      <template v-if="queryParams.wfStatus=== '001' || queryParams.wfStatus=== '004' ">
        <el-button type="primary"
          @click="handleOperation('send','上报', 0)">上报</el-button>
      </template>
      <template v-if="queryParams.wfStatus==='002'">
        <el-button type="danger"
          @click="handleOperation('cancle','撤销', 1)">撤销上报</el-button>
      </template>
      <el-button @click="handleOpenRecord">操作记录</el-button>
    </template>
    <template #aside>
      <rk-ad-ag-tree layoutTree="ag"
        :agConfig="{
            code:'2300',
          }"
        @agNodeClick="handleAgNodeClick">
      </rk-ad-ag-tree>
    </template>
    <template #main>
      <rk-condition>
        <template #content>
          <rk-grid ref="standProReportRef"
            title="备选项目上报"
            dataUrl="/alternateProject/page"
            boxConfig
            :params="queryParams"
            :columns="tableColumns"
            :footerConfig="{
                showFooter:true,
                footerParams:['applyAmt','proCapital','totalBudget']
              }">
            <template #toolbar_title_buttons>
              <el-select v-model="queryParams.dw"
                placeholder="请选择单位"
                class="mr-15px w-200px"
                clearable
                @change="handleSearch">
                <el-option v-for="item in COMMON_AMOUNT_UNIT.options"
                  :key="item.value"
                  :label="item.label"
                  :value="item.value" />
              </el-select>
              <el-date-picker v-model="queryParams.sbYear"
                placeholder="请选择申报年度"
                clearable
                type="year"
                @click="handleSearch" />
            </template>
            <template #status="{ row }">
              <el-tag :type="COMMON_STOP_OPEN_ENUMS.options[row.status].tagType"
                class="mx-1"
                effect="plain">
                {{ COMMON_STOP_OPEN_ENUMS.options[row.status].echo }}
              </el-tag>
            </template>
          </rk-grid>
        </template>
      </rk-condition>
    </template>
  </RkContainer>
  <OperationLog ref="OperationLogRef" />
</template>
    
    <script setup lang="jsx">
import { json_sb_button } from '@/common/button-data.js'
import { COMMON_STOP_OPEN_ENUMS, COMMON_AMOUNT_UNIT } from '@/enums/enums-common.js'
import { WF_TASK_SEND_AUDIT, WF_TASK_AUDIT, WF_TASK_CANCEL, WF_TASK_BACK } from '@/common/cons-common.js'
import { reactive } from 'vue'
import { RkConfirmBox } from '@/components/message-box/index.js'
import { reserveProHandleTask } from '@/api/api-project'
import OperationLog from '@/components/log/OperationLog.vue'
import { useRoute, useRouter } from 'vue-router'

const OperationLogRef = ref(null)
const { appContext } = getCurrentInstance();
const globalProxy = appContext.config.globalProperties;
const standProReportRef = ref(null)
// 查询参数
let queryParams = reactive({
  wfStatus: '001',
  processKey: 'PRO_CBK',
  status: undefined,
  bondTypeCode: undefined,
  nodeId: undefined,
  adCode: '2300',
  agCode: undefined,
  agId: undefined,
  reportYear: undefined,
  mhcx: undefined,
  dw: 2,
})

let route = useRoute()
const router = useRouter()
watch(
  () => router.currentRoute.value.name,
  (newRouterName) => {
    if (newRouterName) {
      queryParams.nodeId = route.query.nodeId
    }
  },
  { immediate: true }
)
onMounted(() => {
  watch(() => queryParams.nodeId, () => {
    handleSearch()
  }, { immediate: false })
})

const tableColumns = ref([
  { field: 'agencyName', title: '申报单位', width: 250, align: 'left', },
  {
    field: 'proName', title: '项目名称', width: 250, align: 'left',
    slots: {
      default: ({ row }) => {
        const title = row.proName || ''
        return title ? [
          <a
            class='yhs-pro-name'
            target='_blank'
            rel='noopener'
            href={`/proj-yhs?proId=${row.proId}&type=st`}>{title}</a>
        ] : ''
      }
    }
  },
  { field: 'proCode', title: '项目编码', width: 200, align: 'left', },
  { field: 'fiscalYear', title: '年度', width: 120, align: 'center', },
  { field: 'debtDeadlineName', title: '债券期限', width: 150, align: 'center', },
  {
    field: 'applyAmt',
    title: '申请金额',
    formatter: 'formatThousand',
    width: 200,
    sumable: true,
    align: 'right'
  },
  {
    field: 'expFuncName', title: '支出功能分类', width: 200, align: 'left'
  },
  {
    field: 'govBgtEcoName', title: '政府预算支出经济分类', width: 200, align: 'left'
  },
  {
    field: 'depBgtEcoName', title: '部门预算支出经济分类', width: 200, align: 'left'
  },
  {
    field: 'proCapital',
    title: '项目资本金',
    formatter: 'formatThousand',
    width: 200,
    sumable: true,
    align: 'right'
  },
  {
    field: 'bondTypeCode', title: '申请类型', width: 150, align: 'left'
  },
  { field: 'applyDate', title: '申报日期', width: 200, align: 'center' },
  { field: 'setupYear', title: '立项年度', width: 180, align: 'center' },
  { field: 'proTypeName', title: '项目类型', width: 150, align: 'left' },
  { field: 'fundInvestAreaCode', title: '资金投向领域', width: 250, align: 'left' },
  {
    field: 'totalBudget',
    title: '项目总概算',
    formatter: 'formatThousand',
    width: 200,
    sumable: true,
    align: 'right'
  },
  { field: 'proStatusName', title: '建设状态', width: 150, align: 'left' },
  { field: 'startDate', title: '开工日期', width: 150, align: 'center' },
  { field: 'endDate', title: '竣工日期', width: 150, align: 'center' },
  { field: 'remark', title: '备注', width: 120, align: 'left' },
])

/**
 * 左侧单位树点击事件，刷新主页面表格
 */
const handleAgNodeClick = ({ data, agTreeNode, }) => {
  console.log('agNodeClick', data, agTreeNode)
  queryParams.agCode = agTreeNode.code
  queryParams.agId = agTreeNode.id
  handleSearch();
}

/**查询 */
const handleSearch = () => {
  standProReportRef.value.searchEvent()
}
//上报、撤销上报、删除
const handleOperation = async (buttonType, textName) => {
  const seleRows = standProReportRef.value.getCheckboxRecords()
  if (seleRows.length == 0 || !seleRows) {
    globalProxy.$message({ message: `请至少选择一条数据进行${textName}！`, type: "warning" });
    return;
  }
  RkConfirmBox({
    title: '提示',
    content: `您确定要${textName}吗？`,
    onSubmit: async function () {
      //获取选中数据
      const ProId = []
      const TaskId = []
      const ApplyId = []
      seleRows.forEach(element => {
        ProId.push(element.proId)
        TaskId.push(element.taskId)
        ApplyId.push(element.applyId)
      });
      const proIds = ProId.join(',')
      const taskIds = TaskId.join(',')
      const applyIds = ApplyId.join(',')
      if (buttonType == 'send') { //上报
        const params = { applyIds, taskIds, proIds, nodeId: queryParams.nodeId, taskType: WF_TASK_AUDIT.value }
        const result = await reserveProHandleTask(params)
        const { data, code, msg } = result
        if (code === 200) {
          globalProxy.$message({ message: `${textName}成功！`, type: "success" });
          handleSearch()
        } else {
          globalProxy.$message({ message: `${msg}`, type: "error" });
        }
      }
      if (buttonType == 'cancle') { //撤销
        const params = { applyIds, taskIds, proIds, nodeId: queryParams.nodeId, taskType: WF_TASK_CANCEL.value }
        const result = await proResApplyCancel(params)
        const { data, code, msg } = result
        if (code === 200) {
          globalProxy.$message({ message: `${textName}成功！`, type: "success" });
          handleSearch()
        } else {
          globalProxy.$message({ message: `${msg}`, type: "error" });
        }
      }
    },
    onCancel: function () {
    }
  })
}
//操作记录
const handleOpenRecord = () => {
  const seleRows = standProReportRef.value.getCheckboxRecords()
  if (seleRows.length == 0 || !seleRows || seleRows.length > 1) {
    globalProxy.$message({ message: "请选择一条数据！", type: "warning" });
    return;
  }
  let applyId = ''
  seleRows.forEach(element => {
    applyId = element.applyId
  });
  OperationLogRef.value.handleOpen(applyId)
}

    </script>
    
    <style lang="scss" scoped>
.mr-15px {
  margin-right: 15px;
  &:last-child {
    margin-right: 0;
  }
}
.w-200px {
  width: 200px;
}
</style>
    